package models;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import models.exceptions.DeckException;

public class Deck 
{
	private static Rank[] ranks = {Rank.TWO, Rank.THREE, Rank.FOUR, Rank.FIVE, Rank.SIX, Rank.SEVEN, 
									 Rank.EIGHT, Rank.NINE, Rank.TEN, Rank.JACK, Rank.QUEEN, Rank.KING, Rank.ACE};
	private static Suit[] suits = {Suit.CLUB, Suit.DIAMOND, Suit.SPADE, Suit.HEART};
	
	public List<Card> cards = new ArrayList<Card>();
	
	public Deck()
	{
		for (Rank r : ranks)
		{
			for (Suit s : suits)
			{
				Card card = new Card(r, s);
				cards.add(card);
			}
		}
		
	}
	
	public void shuffle()
	{
		Collections.shuffle(cards);
	}
	
	public List<Card> giveCards(int amount) throws DeckException
	{
		if(amount > cards.size())
		{
			throw new DeckException("Not enough cards in deck.");
		}
		if(amount < 1)
		{
			throw new DeckException("Amount must be >=1.");
		}
		List<Card> out = new ArrayList<Card>();
		for(int i = 0; i < amount; i++)
		{
			out.add(cards.get(0));
			cards.remove(0);
		}
		return out;	
	}
	
	public void putCards(List<Card> in) throws DeckException
	{
		for(Card i : in)
		{
			if(cards.contains(i))
			{
				throw new DeckException("Card already in deck.");
			}
			cards.add(i);
		}
	}

}
